<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: PHP@妖孽 <dino_ma@163.com>
// +----------------------------------------------------------------------------
/**
 +------------------------------------------------------------------------------
 * 默认脚本
 +------------------------------------------------------------------------------
 * @version   1.0
 * @since 2014-8-8
 +------------------------------------------------------------------------------
 */
namespace Admin\Controller;
class IndexController extends AdminController {
	
    public function index(){
//     	$data = $this->getSay();
    	$this->meta_title= '系统后台首页';
    	$this->display();
    }
    

    
    /**
     * @todo 清除缓存
     */
    public function clearCache(){
    	clearstatcache();
    	rmdirr(RUNTIME_PATH);
    	mkdir(RUNTIME_PATH,0777,true);
    	$obj = \Think\Cache::getInstance(C('DATA_CACHE_TYPE'));
    	$obj->clear();
    	session('admin_menu_list',null);
    	$this->success('清除缓存成功');
    }
    
    
    //测试
    public function test(){
//     	$a = getUserName(1);
//     	$b = getMobilePhone(1);
//     	dump($a);
//     	dump($b);die;
//     	$dataList = array(
//     			array('order_id'=>1,'uid'=>1,'money'=>1,'pay_type'=>1,'log_type'=>1,'create_time'=>0),
//     			array('order_id'=>1,'uid'=>1,'money'=>1,'pay_type'=>1,'log_type'=>1,'create_time'=>0),
//     			array('order_id'=>1,'uid'=>1,'money'=>1,'pay_type'=>1,'log_type'=>1,'create_time'=>0),
//     			array('order_id'=>1,'uid'=>1,'money'=>1,'pay_type'=>1,'log_type'=>1,'create_time'=>0),
//     			array('order_id'=>1,'uid'=>1,'money'=>1,'pay_type'=>1,'log_type'=>1,'create_time'=>0),
//     			array('order_id'=>1,'uid'=>1,'money'=>1,'pay_type'=>1,'log_type'=>1,'create_time'=>0),
//     	);
//     	$a = M('OrderLog')->fetchSql(false)->addAll($dataList);
//     	echo M()->_sql();
//     	M('Menu')->addAll( M('MenuBak')->field('id,title,pid,sort,url,hide,tip,is_dev,status')->where(array('status'=>1))->select());
// 		M('AuthRule')->addAll(M('AuthRuleBak')->field('module,type,name,title,status,condition')->where(array('status'=>1))->select());
//     	$data=array(
//     			'customer_id'=>'2',
//     			'customer_name'=>'4555'
//     	);
//     	$a = M('service_customer_detail')->insertIgnore($data);
// dump($a);
//     	echo M()->_sql();die;
//     	$list = dump(getTeamtName(15));
//     	die;
//     	$list = D('ServiceApi')->getDepartmentMember(1);
//     	dump($list);die;
    	//     	M()->execute("TRUNCATE iweb_service_activity;
// TRUNCATE iweb_service_activity_bus;
// TRUNCATE iweb_service_activity_departure_time;
// TRUNCATE iweb_service_activity_join;
// TRUNCATE iweb_service_activity_join_bus_demand;
// TRUNCATE iweb_service_activity_sale_point;
// TRUNCATE iweb_service_activity_sms;
// TRUNCATE iweb_service_call_log;
// TRUNCATE iweb_service_customer_demand;
// TRUNCATE iweb_service_customer_detail;
// TRUNCATE iweb_service_customer_house;
// TRUNCATE iweb_service_express_detail;
// TRUNCATE iweb_service_my_customer;
// TRUNCATE iweb_service_operation_log;
// TRUNCATE iweb_service_recommend;
// TRUNCATE iweb_service_recommend_customer;
// TRUNCATE iweb_service_roll_call;
// TRUNCATE iweb_service_roll_call_customer;
// TRUNCATE iweb_service_roll_call_member;
// TRUNCATE iweb_service_sms_template;
//     			");
//     	phpinfo();
// 		$obj = new \ZipArchive();
// 		dump($obj);
//     	$list = D('ServiceApi')->getActivityBusList(7);
//     	dump($list);die;
//     	$data = array('13312345678','13312345679','13312345680','13212345677','18812345677','1884567812','18812354321','18686816055','18845678910');
//     	$is = D('customer')->importCustomer($data);
//     	dump($is);die;
//     	$list = D('ServiceApi')->getDepartmentTeamMember(1,3);
//     dump($list);
//     	G('start');
//     	$list = D('ServiceApi')->getAllDemand();
//     	G('end');
//     	echo G('start','end',6);
//     	echo '<hr/>';
//     	echo G('start','end','m');

//     	$field = 'demand_name,status,create_time,update_time';
//     	M('ServiceDemand')->addAll(M('ServiceDemand')->field($field)->select());
//     	die;
//     	$list = D('ServiceApi')->getAllDemand();
//     	dump($list);
//     	M('ServiceDemandContent')->addAll(M('ServiceDemandContent')->field($field)->select());

//     	$list = D('ServiceApi')->getActivitySalePoint(1);
// 		$field='demand_content_name,demand_id,status,create_time,update_time';
//     	M('ServiceDemandContent')->addAll(M('ServiceDemandContent')->field($field)->select());
    	//有用户存在未报名
//     	$activity_id = 11;
//     	$customer = '13263512811';
// 		$customer_id = D('Customer','Service')->customerIsJoinActivity($activity_id,$customer);
// 		if(false == $customer_id){
// 			echo D('Customer','Service')->getError();
// 			die;
// 		}else{
// 			$this->success('操作成功');
//    		}
// 		dump($list);die;
		//用户存在已报名
//     	$activity_id = 11;
//     	$customer = '13263512111';
//     	$list = D('Customer','Service')->customerIsJoinActivity($activity_id,$customer);
//     	if(false == $list){
//     		echo D('Customer','Service')->getError();
//     		die;
//     	}
//     	dump($list);die;
		//用户不存在且未报名
//     	$activity_id = 11;
//     	    	$customer = '13263512121';
//     	    	$list = D('Customer','Service')->customerIsJoinActivity($activity_id,$customer);
//     	    	if(false == $list){
//     	    		echo D('Customer','Service')->getError();
//     	    		die;
//     	    	}
//     	    	dump($list);die;
		
    }
    
//     public function api(){
//     	$data['activity_id']=1;
// 		$data['user_name'] = 'test';
// 		$data['mobile_phone'] = '132635121'.rand('00', '99');
//     	if(D('Activity','Service')->joinActivity($data)){
//     		$this->success('操作成功;');
//     	}else{
//     		$this->error(D('Activity','Service')->getError());
//     	}
    	
//     	return false;
    	
//     	if(D('Admin/Activity')->joinActivity()){
//     		$this->success('操作成功;');
//     	}else{
//     		$this->error(D('Admin/Activity')->getError());
//     	}
//     }
    
	/**
	 * @todo 回收纸质订单
	 * @author sap
	 * @since 2015-11-24
	 */
    public function orderBack(){
    	$file_url = C('DOWNLOAD_UPLOAD.rootPath').'File/roolback_paper_order_list.csv';//文件地址
//     	set_time_limit (0);//程序执行时间
    	//导入phpExcel核心类
    	import( "Common.Msj.PHPExcel.PHPExcel.IOFactory" ,'' ,'.php' );
    	$objReader = \PHPExcel_IOFactory::createReader('CSV');
    	$objReader->setDelimiter(',');//默认的分隔符
    	$objReader->setInputEncoding('GBK');//默认的输入字符集
    	if(!$objReader->canRead($file_url)){
    		$this->error('生成失败! 无法读取该文件数据');
    	}
    	$objPHPExcel = $objReader->load ($file_url);
    	$sheet = $objPHPExcel->getSheet (0);
    	$highestRow = $sheet->getHighestRow (); // 取得总行数

    	if($highestRow < 2){
    		$this->error('空文件');
    		return false;
    	}
    	
    	$data_del = array(); //删除数据初始化
    	$error_num = 0; //失败数
    	
    	for($j = 2; $j <= $highestRow; $j ++) {
    		$key = $j - 2; //添加数组，键
    		$data_del[$key]['start_paper'] = (int)$objPHPExcel->getActiveSheet()->getCell("B" .$j)->getValue();//起点编码
    		$data_del[$key]['end_paper'] = (int)$objPHPExcel->getActiveSheet()->getCell("C" .$j)->getValue();//结尾编码
    		if($data_del[$key]['start_paper']==0 || $data_del[$key]['end_paper']==0){
    			unset ($data_del[$key]);//释放给定的变量
    			$error_num[] = $j;
    		}
    	}
    	if(empty($data_del)){
    		$this->error('空文件');
    		return false;
    	}
    	$data_del = array_merge($data_del);
        //查找order表中是否存在要解绑的纸质订单号，where查询条件拼写
        //select * FROM `iweb_order` WHERE ((`paper_order` BETWEEN 1 AND 100) OR (`paper_order` BETWEEN 1000 AND 1100))
        $map_order = '';$where_order ='';
        foreach ($data_del as $k=>$v){
            if($k == 0){
                $where_order = '(`paper_order` BETWEEN '.$v['start_paper'].' AND '.$v['end_paper'].')';
            }else{
                $where_order = $where_order.' or (`paper_order` BETWEEN '.$v['start_paper'].' AND '.$v['end_paper'].')';
            }
        }
        $map_order = '('.$where_order.')';
        $order_list = M('order')->where($map_order)->field('paper_order')->select();
        //上传的纸质订单号范围
        $arr = array();
        foreach ($data_del as $key=>$val){
            for ($i=$val['start_paper'];$i<=$val['end_paper'];$i++){
                $arr[] = $i;
            }
        }
        //纸质订单号与订单表中的纸质订单号差积
        if(!empty($order_list)){
            $paper_order_arr = array_unique(array_column($order_list, 'paper_order'));
            $arr = array_diff($arr, $paper_order_arr);
        }
        //在iweb_paper_order表中，空白的，在解绑范围内的，并在order表中没有记录纸质订单号，where查询条件拼写
        $map['paper_order_num'] = array('in',$arr);
        $map['order_type'] = 0;
		//删除操作
		$obj = M('paper_order');
		if($obj->where($map)->delete() === false){
			$this->error($obj->getDbError());
		}else{
			$this->success('成功',U('index'));
		}
    }
}